<?php

/**
 * ClientScript class
 *
 * @author Julot
 * @since Friday, June 1, 2012. 11:23 AM
 */
class ClientScript extends CClientScript {

	public function registerScriptContent($id, $alias, $position = 4,
		$params = array()
	) {
		Yii::app()->clientScript->registerCoreScript('jquery');
		
		$path = Yii::getPathOfAlias($alias);
		$content = file_get_contents("$path.js");
		if ($params) {
			$patterns = array();
			$replacements = array();
			foreach ($params as $key=>$value) {
				$patterns[] = "/{$key}(['\"\s?])/";
				$replacements[] = $value . '${1}';
			}
			$content = preg_replace($patterns, $replacements, $content);
		}
		$this->registerScript($id, $content, $position);
	}

}
